package th.ac.tu.engr.timemylife;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDbHelper extends SQLiteOpenHelper {
    private static final String TAG = "MyDbHelper";
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "MyDb.db";

    private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + UsageEntry.Schema.TABLE_NAME + " (" +
        UsageEntry.Schema._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        UsageEntry.Schema.COLUMN_NAME_DATE + " TEXT NOT NULL," +
        UsageEntry.Schema.COLUMN_NAME_DOW + " INTEGER," +
        UsageEntry.Schema.COLUMN_NAME_SYNCED + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR0 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR1 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR2 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR3 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR4 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR5 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR6 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR7 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR8 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR9 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR10 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR11 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR12 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR13 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR14 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR15 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR16 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR17 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR18 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR19 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR20 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR21 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR22 + " INTEGER DEFAULT 0," +
        UsageEntry.Schema.COLUMN_NAME_HOUR23 + " INTEGER DEFAULT 0" +
        ")";
    private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + UsageEntry.Schema.TABLE_NAME;

    /**
     * Initialize SQLite open helper
     * @param context   Application context
     */
    public MyDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * Initialize database
     * @param db        Database instance
     */
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    /**
     * Upgrade obsolete database
     * @param db            Database instance
     * @param oldVersion    Version number
     * @param newVersion    Version number
     */
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    /**
     * Downgrade current database
     * @param db            Database instance
     * @param oldVersion    Version number
     * @param newVersion    Version number
     */
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

}